<template>
    <div
        class="PhysicalCard"
        v-loading="pageLoading"
        element-loading-background="rgba(255, 255, 255, 0.7)"
    >
        <el-card class="box-card">
            <div slot="headerss" class="clearfix text_fl">
                <span>卡操作 --> 虚拟卡更换实体卡</span>
                <el-button class="btn_close" type="text" @click="handleClose">关闭</el-button>
            </div>
            <div>
                <div v-show="!physicalCardSuccess">
                    <el-form ref="formData" :model="formData" :rules="rules" label-width="150px">
                        <el-row>
                            <el-col :span="10" :offset="1">
                                <div class="text_center width30">
                                    <!-- <el-form-item label="账户类别:" class="text_fl">
                                        <span class="el-form-item-span">{{CATEGORY_filter}}</span>
                                    </el-form-item>-->
                                    <el-form-item label="账户类别:" class="text_fl">
                                        <el-select
                                            v-model="formData.CATEGORY"
                                            placeholder="请选择账户类别"
                                            @change="getCATEGORYObj"
                                            disabled
                                        >
                                            <el-option
                                                v-for="item in AccountionTYPE_INFO"
                                                :label="item.label"
                                                :value="item.code"
                                                :key="item.code"
                                            ></el-option>
                                        </el-select>
                                    </el-form-item>
                                </div>
                            </el-col>
                            <el-col :span="12">
                                <div class="text_center width30">
                                    <!-- <el-form-item label="卡号:" class="text_fl">
                                        <el-input :disabled="true" v-model.trim="formData.CARDNO"></el-input>
                                    </el-form-item>-->
                                    <el-form-item label="卡号:" class="text_fl">
                                        <el-select
                                            v-model.trim="formData.CARDNO"
                                            placeholder="请选择卡号"
                                            @change="getCARDNBRFun"
                                            disabled
                                        >
                                            <el-option
                                                v-for="item in cardList_option"
                                                :label="item.CARDNBR"
                                                :value="item.CARDNBR"
                                                :key="item.CARDNBR"
                                            ></el-option>
                                        </el-select>
                                    </el-form-item>
                                </div>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="10" :offset="1">
                                <div class="text_center width30">
                                    <el-form-item label="递卡方式:" class="text_fl" prop="REPLACEFEE">
                                        <el-select
                                            v-model="formData.REPLACEFEE"
                                            placeholder="请选择递卡方式"
                                            clearable
                                            disabled
                                        >
                                            <el-option
                                                v-for="item in isREPLACEFEEOption"
                                                :label="item.label"
                                                :value="item.code"
                                                :key="item.label"
                                            ></el-option>
                                        </el-select>
                                    </el-form-item>
                                </div>
                            </el-col>
                            <el-col :span="12">
                                <div class="text_center width30">
                                    <el-form-item label="凸字姓名(姓/名/):" class="text_fl">
                                        <el-input
                                            v-model="formData.EMBNAME"
                                            class="textSpan"
                                            placeholder=" 姓/名/"
                                        ></el-input>
                                    </el-form-item>
                                </div>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="10" :offset="1">
                                <div class="text_center width30">
                                    <el-form-item label="补卡费用:" class="text_fl">
                                        <el-input :disabled="true" v-model.trim="COST_value"></el-input>
                                    </el-form-item>
                                </div>
                            </el-col>
                            <el-col :span="12">
                                <div class="text_center width30">
                                    <el-form-item label="卡片寄送地址类型:" class="text_fl" prop="CARDTO">
                                        <el-select
                                            v-model="formData.CARDTO"
                                            placeholder="请选择递卡地址类型"
                                            clearable
                                            disabled
                                        >
                                            <el-option
                                                v-for="item in isCARDTOOption"
                                                :label="item.label"
                                                :value="item.code"
                                                :key="item.label"
                                            ></el-option>
                                        </el-select>
                                    </el-form-item>
                                </div>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="10" :offset="1">
                                <div class="text_center width30">
                                    <el-form-item label="电子现金余额:" class="text_fl">
                                        <span class="el-form-item-span">{{BALANCE_value}}</span>
                                    </el-form-item>
                                </div>
                            </el-col>
                            <el-col :span="12">
                                <div class="text_center width30">
                                    <el-form-item label="是否绑定ETC:" class="text_fl">
                                        <span class="el-form-item-span"></span>
                                    </el-form-item>
                                </div>
                            </el-col>
                        </el-row>
                        <!-- 话术 -->
                        <div class="hstable_wrap" v-if="speechList.length!=0">
                            <el-table
                                :data="speechList"
                                border
                                stripe
                                header-row-class-name="headerRow"
                                ref="showListTable"
                                class="heard-bg business_table hstable"
                                @selection-change="handleSelectionChange"
                            >
                                <el-table-column type="selection" label=" " min-width="55"></el-table-column>
                                <el-table-column label="业务话术">
                                    <template slot-scope="scope">{{scope.row.content}}</template>
                                </el-table-column>
                            </el-table>
                        </div>
                        <div class="text_center">
                            <el-button
                                type="primary"
                                size="small"
                                @click.native="submitFormReissueCard"
                                icon="el-icon-circle-check"
                            >确定</el-button>
                            <el-button
                                type="primary"
                                size="small"
                                icon="el-icon-refresh-right"
                                @click="handleClickRemove"
                            >重置</el-button>
                        </div>
                    </el-form>
                    <el-dialog
                        v-drag
                        title="虚拟卡更换实体卡"
                        :visible.sync="centerDialogVisible"
                        width="500px"
                        :append-to-body="true"
                        top="30vh"
                        :close-on-click-modal="false"
                        center
                    >
                        <p>
                            卡号：
                            <span>{{this.formData.CARDNO}}</span>
                        </p>
                        <p>
                            卡产品：
                            <span>{{this.PRODUCT_value}}</span>
                        </p>
                        <p>
                            递卡方式：
                            <span>{{this.formData.REPLACEFEE|filterrep}}</span>
                        </p>
                        <p>
                            收费标准：
                            <span>{{this.COST_value}}</span>
                        </p>
                        <p>
                            寄送地址：
                            <span>{{this.formData.CARDTO|filtercardto}}</span>
                        </p>
                        <p v-if="BALANCE_value!=='0'">
                            电子现金余额：
                            <span>{{this.BALANCE_value}}</span>
                        </p>
                        <!-- <p v-if="ETCSTATUS_value">
                            是否绑定ETC：
                            <span>{{this.ETCSTATUS_value}}</span>
                        </p>-->
                        <p class="text_center padding_top15">
                            <span style="color:#3a8ee6; ">您确定对该卡号进行虚拟卡更换实体卡操作？</span>
                        </p>
                        <span slot="footer" class="dialog-footer">
                            <el-button
                                size="small"
                                icon="el-icon-circle-close"
                                @click="centerDialogVisible = false"
                            >取 消</el-button>
                            <el-button
                                type="primary"
                                size="small"
                                @click="handerCardReissue"
                                icon="el-icon-circle-check"
                            >确定</el-button>
                        </span>
                    </el-dialog>
                </div>
                <div v-show="physicalCardSuccess">
                    <physical-card-item
                        v-if="physicalCardSuccess"
                        :physicalCardSuccess="physicalCardSuccess"
                        :CARDTO_Value="formData.CARDTO"
                    ></physical-card-item>
                </div>
            </div>
        </el-card>
    </div>
</template>
<script>
import { mapGetters } from 'vuex';
import PhysicalCardItem from './components/PhysicalCardItem.vue';
import {
    closePage,
    getDictSelectList,
    itoStr,
    getDictMappingValue,
    componentCost,
    setICCardInfo,
    setETCCardInfo,
    updateDeal3004,
    getCardInfo3004,
    updateDeal3601,
} from '@/utils/Common';

export default {
    name: 'PhysicalCard',
    data() {
        return {
            formData: {
                EMBNAME: '',
                CARDTO: '', // /卡片递送地址类型
                REPLACEFEE: '0',
                DSPCHCD: 'POST', // this.newCardInfo_3004.DESPATCH,//递卡方式
                ISSRSN: 'L', // 换卡原因 新账户发卡
                CHECKFLG: '000000000000000', // 检查项目选择
                CDFRM: 'A', // 卡片版面  必填字段 可以给四个空格
                PINREQD: '0', // 是否产生PIN 1:不产生PIN 0:产生PIN
                KEEPYN: '0', // 换卡保留卡号选项 0不保留 1保留
                CDISSFLAG: '0', // 即时制卡 0：否（默认）1：是
                PINFLAG: this.pinFlag,
                VCNYN: '2',//0-默认 1-虚拟卡 2-实体卡
            },
            // isDSPCHCDOption: [], // 递卡方式 isDESPATCHOption DSPCHCD
            isCARDTOOption: [], // 卡片递送地址类型
            isREPLACEFEEOption: [], // 换卡费用代码选项
            // isCOURIERFEEOption: [], // 递送费用代码选项
            physicalCardSuccess: '', //更换成功后的信息
            centerDialogVisible: false, // 弹框是否显示
            PRODUCT_value: '', // //卡产品 在数据字典里的值
            CATEGORY_filter: '',
            productId: '',
            // EMBNAME_filter: '', // 凸字姓名
            rules: {
                //EMBNAME: [{ required: true, message: '凸字姓名不能为空！', trigger: 'blur' }],
                DSPCHCD: [{ required: true, message: '递卡方式不能为空！', trigger: 'change' }],
                // CARDTO: [
                //     { required: true, message: '卡片递送地址类型不能为空！', trigger: 'blur' },
                // {type:'string',message:'卡片递送地址类型为字符串',whitespace:true}
                // ],
                // COURIERFEE: [{ required: true, message: '递送费用不能为空！', trigger: 'change' }],
                REPLACEFEE: [{ required: true, message: '补卡费用不能为空！', trigger: 'change' }],
            },
            Card_STAT: '',
            pageLoading: false,
            Product_option: '',//所有的产品列表
            ChangeProduct_option: '',//可以换实体卡的产品列表
            BALANCE_value: '',//ic  3096的BALANCE 值
            ETCSTATUS_value: false,//3134ETC签约关系查询交易接口
            COST_value: '0',//补卡费用 单纯用来展示 计算后的补卡费用
            cardtype: '',//补卡费用字典
            PRODUCT: '',//
            newCardInfo_3004: '',//每次换卡的默认卡信息
            cardList_option: '',//当前账户下的所有的卡
            speechList: [],//话术列表
            selectedList: [],
            MenuId: '',
        };
    },
    filters: {

        // 
        filterrep(val) {
            if (val === '0') {
                return '免费';
            } else if (val === '1') {
                return '普通';
            } else if (val === '2') {
                return '加急';
            }
        },
        filtercardto(val) {
            if (val === 'H') {
                return '家庭地址';
            } else if (val === 'B') {
                return '公司地址';
            } else {
                return '';
            }
        },
    },
    components: {
        PhysicalCardItem,
    },
    computed: {
        ...mapGetters([
            'cardInfo_3004',
            'customerAdressInfo_3003',
            'defaultParams',
            'pinFlag',
            'AccountionTYPE_INFO',
            'cardList_3601',
            'isCurrentMenuInfo',
        ]),
    },
    async mounted() {
        this.MenuId = this.$route.params.id || '';
        this.pageLoading = true;
        this.setDefaultCardInfo();
        await this.getViewInit()
        // dspchcd cardto   
        this.getDictInfovalue();
        //  this.isREPLACEFEEOption=this.getDictInfo("COURFEE");
        //  this.isCOURIERFEEOption=this.getDictInfo("REPFEE");
        // 3096IC卡交易明细查询
        setICCardInfo(this.formData.CARDNO, this);
        // 3134ETC签约关系查询交易接口
        //setETCCardInfo(this.formData.CARDNO, this);
    },

    methods: {
        // 修改默认交易
        setDefaultCardInfo() {
            // 这个方法只会调用一次
            this.pageLoading = true;
            if (!this.newCardInfo_3004) {
                this.newCardInfo_3004 = JSON.parse(JSON.stringify(this.cardInfo_3004));
            };
        },
        // 获取账户类型
        getCATEGORYObj(val) {
            console.log('val', val);
            // 获取账户类型
            this.AccountionTYPE_INFO.some(async (item) => {
                if (item.code === val) {
                    this.CATEGORY_filter = item.label;
                    return true
                }
            });
            console.log('this.CATEGORY_filter', this.CATEGORY_filter);
            //获取当前账户类型下 所有的卡 
            this.getCardListOption(val);

        },
        //获取当前账户类型下 所有的卡 
        getCardListOption(CATEGORY_val) {
            this.cardList_option = this.cardList_3601.filter((item) => (item.CATEGORY + '').trim() === CATEGORY_val);
            console.log(" this.cardList_option ", this.cardList_option);
            if (this.cardInfo_3004.CATEGORY + '' === CATEGORY_val) {
                this.formData.CARDNO = this.cardInfo_3004.CARDNO;  // 卡号
                console.log('this.formData.CARDNO', this.formData.CARDNO);
            } else {
                this.formData.CARDNO = '';
            }
        },
        // 选择 选中的需要操作的卡 后
        async getCARDNBRFun(cardNo) {
            // 选中 需要操作的卡后 需要重新跑 3004接口
            console.log("cardNo", cardNo);
            this.formData.CARDNO = cardNo;
            // 选中 需要操作的卡后 需要重新跑 3004接口
            console.log("cardNo", cardNo);
            this.formData.CARDNO = cardNo;
            // 更新 newCardInfo_3004 里的值
            await getCardInfo3004(cardNo, this);
            this.getNew3004();

        },
        async getNew3004() {
            this.formData.CATEGORY = this.newCardInfo_3004.CATEGORY + ''; // 账户类别
            this.formData.CARDNO = this.newCardInfo_3004.CARDNO; // 卡号
            this.formData.CARDNBR = this.newCardInfo_3004.CARDNO; // 卡号
            this.formData.EMBNAME = this.disPoseName(this.newCardInfo_3004.EMBNAME); // 凸字姓名
            this.formData.ISSUENBR = this.newCardInfo_3004.ISSUENBR; // 换卡次数
            this.formData.COURFEE = `${this.newCardInfo_3004.COURIERFEE}`; // 递送费用代码
            this.formData.REPFEE = `${this.newCardInfo_3004.REPLACEFEE}`; // 换卡费用代码
            this.Card_STAT = (this.newCardInfo_3004.CARDSTAT + '').trim();
            this.productId = this.newCardInfo_3004.PRODUCT;
            this.PRODUCT = (this.newCardInfo_3004.PRODUCT + '').trim();
            console.log('产品号换了', this.PRODUCT);
            // 账户类型
            this.productId = itoStr(this.newCardInfo_3004.PRODUCT, 4);
            this.CATEGORY_filter = await getDictMappingValue('CATE', this.newCardInfo_3004.CATEGORY, this);
            // 卡产品
            this.PRODUCT_value = await getDictMappingValue('PRODUCT', this.newCardInfo_3004.PRODUCT, this);
            // console.log('卡号换了', this.newCardInfo_3004.PRODUCT);
            // console.log('卡产品也换了', this.PRODUCT_value);
        },
        async getViewInit() {

            this.formData.CARDTO = `${this.customerAdressInfo_3003.STSMAIL.trim()}`, // /卡片递送地址类型, // /卡片递送地址类型
                this.getNew3004();
            this.getCardListOption(this.formData.CATEGORY);
            await this.getspeechList();
        },
        // 请求数据字典里的字段
        async getDictInfovalue() {

            // 递卡方式
            // this.isDSPCHCDOption = await getDictSelectList('DSPCHCD', this);
            // 寄送地址类型
            this.isCARDTOOption = await getDictSelectList('MAILINGADDRESS', this);
            // 换卡费用代码
            this.isREPLACEFEEOption = await getDictSelectList('REPFEE', this);
            // 递送费用代码
            // this.isCOURIERFEEOption = await getDictSelectList('COURFEE', this);

            // PRODUCT 卡片产品list
            this.Product_option = await getDictSelectList('PRODUCT', this);
            //  可进行卡片更换的卡片产品list
            this.ChangeProduct_option = await getDictSelectList('CHANGE_PRODUCT', this);

            // 补卡费用代码
            this.cardtype = await getDictSelectList('CARDTYPE', this);
            this.getProductvalue(this.Product_option);
            this.getExecuteFun();
            // this.disPoseName(this.newCardInfo_3004.EMBNAME, this.newCardInfo_3004.EMBOSSSUL, this.newCardInfo_3004.EMBOSSSUR);
            this.pageLoading = false;

        },
        //根据菜单id查询话术列表
        async getspeechList() {
            console.log("this.isCurrentMenuInfo.id", this.isCurrentMenuInfo.id);
            console.log("this.MenuId", this.MenuId);
            var checkParam = {
                'id': this.MenuId || this.isCurrentMenuInfo.id,
            };
            this.$post.busiMenu.getlistByMenuId(checkParam).then((res) => {
                console.log("话术查询结果：", res);
                if (res.status === 200) { // http通讯成功
                    if (res.data.status == 0) { // 业务请求成功
                        this.speechList = res.data.content;
                    }
                }
            });
        },
        handleSelectionChange(val) {
            this.selectedList = val;
        },
        // 获取Product option里过滤的值
        getProductvalue(PRODUCTOption) {
            PRODUCTOption.forEach(element => {
                if (element.code === this.productId) {
                    this.PRODUCT_value = element.label;
                    return;
                }
            });
        },
        async getExecuteFun() {
            //当前卡片状态 为销卡状态才可以 虚拟卡变更实体卡 

            // 虚拟卡可以进行变更操作！
            if (this.newCardInfo_3004.VCNSTS.trim() !== 'A') {
                this.$alert(
                    `当前卡不是虚拟卡，不可进行变更操作！`,
                    '虚拟卡更换实体卡',
                    { customClass: 'warning', type: 'warning', showClose: false },
                ).then(() => {
                    closePage(this.$route, this, 1);
                });
            } else {
                // 获取当前不是汽车卡和配置的卡 他们的类型 然后弹出提示  
                // ChangeProduct_option 为配置项 后期可配置其他卡类型进来 前端配置 不需要发版生效
                if (!this.ChangeProduct_option.some((item) => item.code === this.productId)) {
                    let productId_label = '';
                    this.ChangeProduct_option.filter((item) => {
                        productId_label = productId_label + item.label + ',';
                    })
                    this.$alert(
                        `当前卡不是${productId_label}不可进行变更操作！`,
                        '虚拟卡更换实体卡',
                        { customClass: 'warning', type: 'warning', showClose: false },
                    ).then(() => {
                        closePage(this.$route, this, 1);
                    });
                }
            };

        },
        // 开始虚拟卡变更实体卡
        handerCardReissue() {
            this.setsubmitForm();
            this.centerDialogVisible = false;
        },
        // 从数据字典里查  产品编号对应的文字
        submitFormReissueCard() {

            this.$refs.formData.validate((valid) => {
                if (valid) {
                    console.log("this.selectedList.length", this.selectedList.length);
                    if (this.selectedList.length != this.speechList.length) {
                        this.$alert('话术尚未全部勾选！', '提示', {
                            customClass: 'warning', type: 'warning',
                        });
                        return;
                    }
                    this.centerDialogVisible = true;
                }
            });
        },

        // 处理 凸字姓名
        // name 凸字姓名  number 截取位数
        disPoseName(name) {
            const number = this.newCardInfo_3004.EMBOSSSUL;
            const number2 = this.newCardInfo_3004.EMBOSSSUR
            let nameValue = '';
            nameValue = name.substring(0, number - 1);
            nameValue = `${nameValue}/${name.substring(number - 1, number2)}`;
            nameValue = `${nameValue}/${name.substring(number2)}`;
            nameValue && (nameValue = (`${nameValue}`).trim());
            return nameValue;
        },
        // 下拉选 触发事件
        componentCostFun(val) {
            componentCost(val, this);
        },
        // 发送请求 开始虚拟卡更换实体卡
        setsubmitForm() {
            const defaultParams = JSON.parse(JSON.stringify(this.defaultParams));
            defaultParams.body.appBody = Object.assign(this.defaultParams.body.appBody, this.formData);
            defaultParams.body.appHead.tranCode = '4025';// 修改交易号
            console.log(defaultParams);
            this.$post.common.transPort(defaultParams).then((res) => {
                if (res.status === 200) { // 请求成功
                    const cardlossofs = JSON.parse(res.data.jsonRes);
                    if (cardlossofs.sysHead.rspCd == '000000') { // 交易成功
                        console.log(cardlossofs, 'cardlossofs');
                        this.physicalCardSuccess = cardlossofs;
                        this.physicalCardSuccess.CATEGORY_filter = this.CATEGORY_filter;
                        this.physicalCardSuccess.CARDNO = this.formData.CARDNO;
                        this.physicalCardSuccess.CARDTO_Value = this.formData.CARDTO;
                        if (this.newCardInfo_3004.CARDNO === this.cardInfo_3004.CARDNO) {
                            updateDeal3004(this.newCardInfo_3004.CARDNO, this);
                        }
                        updateDeal3601(this.newCardInfo_3004.CARDNO, this.newCardInfo_3004.KEYTYPE, this.newCardInfo_3004.CUSTID, this);
                    } else {
                        const ErrorMessage = `错误码：${cardlossofs.sysHead.rspCd} ` + `错误信息：${cardlossofs.sysHead.rspMsg}`;
                        this.$alert(ErrorMessage, cardlossofs.sysHead.busiName, { customClass: 'error', type: 'error' });
                    }
                }
                this.centerDialogVisible = false; // 关闭弹框
            });
        },
        // 重置 功能
        handleClickRemove() {
            this.formData.LOCLIMIT = '';
        },
        // 点击关闭
        handleClose() {
            closePage(this.$route, this, 1);
        },
    },
};
</script>
<style lang='scss' >
.PhysicalCard {
    /* .el-form-item-span {
        background-color: #f5f7fa;
        border-color: #e4e7ed;
        color: #606266;
        cursor: not-allowed;
        width: 250px;
        min-width: 200px;
        height: 35px;
        line-height: 35px;
        display: inline-block;
        background-image: none;
        border-radius: 4px;
        border: 1px solid #dcdfe6;
        box-sizing: border-box;
        padding: 0 15px;
    } */
    .el-card__header {
        padding: 8px !important;
    }
    .el-form-item__content {
        text-align: left;
    }

    .el-dialog__body p {
        text-align: left;
        padding-left: 30%;
        margin: 8px 5px;
        span {
            color: #3a8ee6;
        }
    }
    .el-card.is-always-shadow {
        -webkit-box-shadow: none;
        box-shadow: none;
    }
    .el-card {
        border: none;
    }
}
</style>

<style lang="scss" scope>
</style>